package com.xspace.android.xafcommon.util;

import android.content.Context;
import android.text.TextUtils;

import java.text.DecimalFormat;

/**
 * 字符串处理类
 *
 * @author zhoujunwei
 * @date 2015-4-23 13:44:41
 */
public class StringToolkit {
    public static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0;
    }


    /**
     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
     *
     * @param s 需要得到长度的字符串
     * @return int 得到的字符串长度
     */
    public static int length(String s) {
        if (s == null)
            return 0;
        char[] c = s.toCharArray();
        int len = 0;
        for (int i = 0; i < c.length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }


    /**
     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为1,英文字符长度为0.5
     *
     * @param s 需要得到长度的字符串
     * @return int 得到的字符串长度
     */
    public static double getLength(String s) {
        double valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        // 获取字段值的长度，如果含中文字符，则每个中文字符长度为1，否则为0.5
        for (int i = 0; i < s.length(); i++) {
            // 获取一个字符  
            String temp = s.substring(i, i + 1);
            // 判断是否为中文字符  
            if (temp.matches(chinese)) {
                // 中文字符长度为1  
                valueLength += 1;
            } else {
                // 其他字符长度为0.5  
                valueLength += 0.5;
            }
        }
        //进位取整  
        return Math.ceil(valueLength);
    }

    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    /**
     * 按照给出的长度截取中英文混合字符串
     *
     * @param length 需要截取字符串的长度
     * @param s
     * @return
     */
    public static String getSubStringByLength(int length, String s) {
        String temp = "";
        if (TextUtils.isEmpty(s)) return temp;
        if (s.length() < length) return s;
        char[] charArr = s.toCharArray();
        int count = 0;
        int location = 0;
        for (int i = 0; i < charArr.length; i++) {
            char tp = charArr[i];
            if (isChinese(tp)) {
                count += 2;
            } else {
                count++;
            }
            location = i;
            if (count >= length * 2) {
                break;
            }
        }

        temp = s.substring(0, location);

        //Log.i("toolkit", "getSubStringByLength s:"+s+" \n temp:"+temp);
        return temp;
    }

    /**
     * 根据手机的分辨率从dp 的单位 转成为px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从px(像素) 的单位 转成为dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 转换文件大小
     *
     * @param fileS
     * @return
     */
    public static String FormatFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        fileSizeString = fileSizeString.replace(".00", "");
        return fileSizeString;
    }
}
